<template>
  <Layout>
    <div class="publish-detail-page max-width-1500">
      <div class="header">
        <el-page-header 
          title="返回"
          content="发布详情"
          @back="goBack"
        />
      </div>

      <div class="publish-detail" v-if="publishData">
        <div class="main-content">
          <div class="left-column">
            <div class="publish-content">
              <div class="publish-header">
                <el-avatar 
                  :size="40" 
                  :src="publishData.author?.avatar"
                >
                  {{ publishData.author?.name?.charAt(0) }}
                </el-avatar>
                <div class="publish-author">
                  <div class="author-name">{{ publishData.author?.name }}</div>
                  <div class="publish-time">{{ formatDate(publishData.createdAt) }}</div>
                </div>
              </div>
              
              <div class="publish-text">
                {{ publishData.content }}
              </div>
              
              <div v-if="publishData.images && publishData.images.length > 0" class="publish-images">
                <el-image
                  v-for="(image, index) in publishData.images"
                  :key="index"
                  :src="image.fileurl"
                  class="image-item"
                  :preview-src-list="publishData.images.map(img => img.fileurl)"
                  :initial-index="index"
                  fit="cover"
                />
              </div>
              
              <div class="publish-meta">
                <el-descriptions :column="2" border>
                  <el-descriptions-item label="发布ID">{{ publishData.publish_id }}</el-descriptions-item>
                  <el-descriptions-item label="作者">{{ publishData.author?.name || '未知' }}</el-descriptions-item>
                  <el-descriptions-item label="创建时间" :span="2">{{ formatDate(publishData.createdAt) }}</el-descriptions-item>
                  <el-descriptions-item label="可见性">{{ publishData.is_public ? '公开' : '私有' }}</el-descriptions-item>
                  <el-descriptions-item label="点赞数">{{ publishData.like_count }}</el-descriptions-item>
                  <el-descriptions-item label="评论数">{{ publishData.comment_count }}</el-descriptions-item>
                </el-descriptions>

                <div class="user-actions">
                  <PublishUserLike 
                    :publish="publishData" 
                    @like-change="handleLikeChange" 
                  />
                </div>
              </div>
            </div>
          </div>

          <div class="right-column">
            <!-- 评论功能 -->
            <div class="comment-section">
              <PublishUserComment 
                :publish="publishData" 
                @comment-change="handleCommentChange" 
              />
            </div>
          </div>
        </div>
      </div>

      <div class="loading-container" v-else-if="loading">
        <el-skeleton animated>
          <template #template>
            <div style="margin-top: 20px;">
              <el-skeleton-item variant="text" style="width: 30%;" />
              <el-skeleton-item variant="text" style="width: 50%; margin-top: 10px;" />
              <el-skeleton-item variant="text" style="width: 70%; margin-top: 10px;" />
            </div>
          </template>
        </el-skeleton>
      </div>

      <div class="empty-container" v-else>
        <el-empty description="未找到发布数据" />
      </div>
    </div>
  </Layout>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { getPublishById, type Publish } from '@/api/modules/publish'
import Layout from '@/components/Layout.vue'
import UserProfileForm from '@/components/UserProfileForm.vue'
import PublishUserLike from '@/views/Console/PublishManagement/PublishUserLike.vue'
import PublishUserComment from '@/views/Console/PublishManagement/PublishUserComment.vue'

const route = useRoute()
const router = useRouter()

// 发布数据
const publishData = ref<Publish | null>(null)

// 加载状态
const loading = ref(false)

// 格式化日期
const formatDate = (dateString: string | undefined) => {
  if (!dateString) return '';
  const date = new Date(dateString);
  return date.toLocaleString('zh-CN');
};

// 返回上一页
const goBack = () => {
  router.push('/console/publish-management')
}

// 获取发布详情
const fetchPublishDetail = async (publishId: number) => {
  loading.value = true
  try {
    const response = await getPublishById({ publish_id: publishId })
    publishData.value = response.data || null
  } catch (error) {
    console.error('获取发布详情失败:', error)
  } finally {
    loading.value = false
  }
}

// 处理点赞数变化
const handleLikeChange = (likeCount: number) => {
  if (publishData.value) {
    publishData.value.like_count = likeCount
  }
}

// 处理评论数变化
const handleCommentChange = (commentCount: number) => {
  if (publishData.value) {
    publishData.value.comment_count = commentCount
  }
}

// 组件挂载时获取发布详情
onMounted(() => {
  const publishId = Number(route.params.id)
  if (publishId) {
    fetchPublishDetail(publishId)
  } else {
    goBack()
  }
})
</script>

<style scoped>
.publish-detail-page {
  padding: 20px;
  background: white;
  border-radius: 4px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  height: 100%;
  overflow: auto;
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.publish-detail {
  height: calc(100% - 60px);
}

.main-content {
  display: flex;
  gap: 20px;
  height: calc(100% - 60px);
  overflow: auto;
}

.left-column {
  width: 70%;
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.right-column {
  width: 30%;
}

.publish-content {
  width: 75%;
}

.publish-header {
  display: flex;
  align-items: center;
  margin-bottom: 15px;
}

.publish-author {
  margin-left: 10px;
}

.author-name {
  font-weight: bold;
  font-size: 14px;
}

.publish-time {
  font-size: 12px;
  color: #909399;
}

.publish-text {
  margin-bottom: 15px;
  white-space: pre-wrap;
  word-break: break-word;
}

.publish-images {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));
  gap: 10px;
  margin-bottom: 15px;
}

.image-item {
  width: 100%;
  height: 120px;
  border-radius: 4px;
}

.publish-meta {
  margin-top: 20px;
}

.user-actions {
  margin: 20px 0;
  text-align: left;
}

.comment-section {
  height: 100%;
  display: flex;
  flex-direction: column;
}

.loading-container,
.empty-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 400px;
}

@media (max-width: 768px) {
  .main-content {
    flex-direction: column;
  }
  
  .header {
    flex-direction: column;
    align-items: flex-start;
    gap: 10px;
  }
  
  .header-actions {
    align-self: flex-end;
  }
}
</style>